package org.oa.ai.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.oa.ai.chatModel.CcCtCwyjyy;
import org.oa.ai.chatModel.Ccycsq;
import org.oa.ai.chatModel.OactbdCsqjsq;
import org.oa.ai.feign.AIFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CommonToolsService {

    @Autowired
    private AIFeignService aiFeignService;

    @Tool("保存用车申请")
    public Map<String, Object> submitYcsq(
            @P("申请人部门") String bm,
            @P("申请人") String sqr,
            @P("申请人电话") String lxfs,
            @P("乘车人数") Integer ycrs,
            @P("用车人") String ycr,
            @P("用车人电话") String ycrdh,
            @P("车型需求") String cx,
            @P("车型需求其他选项说明") String qita,
            @P("出车时间") String sjcc,
            @P("用车事由及要求") String ycsy,
            @P("行驶路线") String xslx,
            @P("费用出处") String fycc,
            @P("费用出处专项费用说明") String xmmc,
            @P("账号id") String userId,
            @P("部门id") String deptId,
            @P("当前部门") String loginDeptName
    ) {
        Ccycsq ccycsq = new Ccycsq();
        ccycsq.setTitle("用车申请【"+ycsy+"】");  // 业务名称+用车事由
        ccycsq.setSqr(sqr);
        ccycsq.setBm(bm);
        ccycsq.setLxfs(lxfs);
        ccycsq.setYcrs(ycrs);
        ccycsq.setYcr(ycr);
        ccycsq.setYcrdh(ycrdh);
        ccycsq.setCx("\""+cx+"\"");
        ccycsq.setQita(qita);
        ccycsq.setSjcc(sjcc);
        ccycsq.setXslx(xslx);
        ccycsq.setFycc("\""+fycc+"\"");
        ccycsq.setXmmc(xmmc);
        ccycsq.setCreateuserid(userId);
        ccycsq.setCreatedeptname(loginDeptName);
        ccycsq.setCreatedeptid(deptId);
        ccycsq.setMjid("10");
        ccycsq.setMjname("公开");
        ccycsq.setYwlx("ycsq1");
        ccycsq.setYwname("用车申请");
        ccycsq.setStatus("10");
        ccycsq.setYcsy(ycsy);
        Map<String, Object> bean = JSONUtil.toBean(JSONUtil.toJsonStr(ccycsq), Map.class);
        try {
            Map<String, Object> result = aiFeignService.addForm(bean);
            return Map.of(
                    "新增后的表单数据", JSONUtil.toBean(JSONUtil.toJsonStr(result.get("formData")), CcCtCwyjyy.class),
                    "表名称", result.get("tableName")
            );
        }catch (Exception e) {
//            return Map.of("result", "保存成功！");
            return Map.of("result", "保存失败！");
        }
    }

    @Tool("获取用车人列表")
    public JSONArray getYcrList(@P("当前部门id") String login_dept_id) {
        // 通过请假数据id查询数据
        JSONArray jsonArray = aiFeignService.getYcrList(login_dept_id);
        return jsonArray;
    }

    @Tool("获取流程节点")
    public JSONArray getFlowNodeList(@P("用车申请id") String id, @P("表名称") String tableName,  @P("当前用户id") String login_user_id) {
        // 通过请假数据id查询数据
        Map<String, Object> map = aiFeignService.getDataByIdByCC(tableName, id);
        String ywlx = map.get("ywlx").toString();
        JSONArray obj = aiFeignService.nextTasksByNodeIdCC("", ywlx, login_user_id);
        return obj;
    }

    @Tool("获取流程节点审批人")
    public JSONArray getFlowUserByNodeIdNew (@P("用车申请id") String id, @P("表名称") String tableName,@P("当前用户id") String login_user_id, @P("流程节点id") String nodeId) {
        Map<String, Object> map = aiFeignService.getDataByIdByCC(tableName, id);
        String ywlx = map.get("ywlx").toString();
        JSONArray userList = aiFeignService.getFlowUserByNodeIdCC(nodeId, ywlx, login_user_id, login_user_id,login_user_id);
        return userList;
    }

    @Tool("提交流程")
    public String taskComplete(
            @P("用车申请id") String id,
            @P("表名称") String tableName,
            @P("流程节点id") String nodeId,
            @P("流程节点名称") String nodeName,
            @P("提交的用户id") String userIds,
            @P("提交的用户名称") String userNames
    ) {
        Map<String, Object> map = aiFeignService.getDataByIdByCC(tableName, id);
        // 调用流程启动接口
        String businessKey = map.get("id").toString();
        String processDefinitionKey = map.get("ywlx").toString();
        String title = map.get("title").toString();
        String moduleId = "configureBusiness";  // 综合办公的modelId
        String firstNode = nodeId;
        String reportStatus = "ptyg";
        String mijiid = map.get("mjid").toString();
        String comminfoId = "";
        String formTyp = map.get("formtype").toString();
        // 流程启动
        Map<String, Object> flowStartMap = aiFeignService.startProcessByKeyNewCC(businessKey, processDefinitionKey, title, moduleId, firstNode, reportStatus, mijiid, tableName, comminfoId, map.get("createuserid").toString(), formTyp);
        // 获取任务信息
        Map<String, Object> taskInfo = (Map<String, Object>) flowStartMap.get("taskInfo");
        if (taskInfo != null) {
            String taskId = taskInfo.get("taskId").toString();
            // 修改流程状态
            Boolean b = aiFeignService.updateReportStatus(taskId, "tjz");
            // 如果修改成功，则流程提交
            if (b) {
                String currentTaskId = taskId;
                String nextTaskActivityId = nodeId;
                String nextTaskUserIds = userIds;
                String nextTaskUsernames = userNames;
                String assigneeId = userIds;
                String nodeCode = "";
                String rwlxs = "ptyg";
                String exField5 = "true";
                String processDefinitionId = "";
                String currentNodeId = "";
                String tjlx = "";
                String logtype = "";
                // 提交流程
                Map<String, Object> result = aiFeignService.taskCompleteNew(
                        businessKey,
                        currentTaskId,
                        nextTaskActivityId,
                        nextTaskUserIds,
                        nextTaskUsernames,
                        assigneeId,
                        moduleId,
                        nodeName,
                        reportStatus,
                        nodeCode,
                        rwlxs,
                        exField5,
                        processDefinitionId,
                        currentNodeId,
                        tjlx,
                        logtype,
                        tableName,
                        comminfoId,
                        processDefinitionKey,
                        formTyp,
                        map.get("createuserid").toString()
                );
            }
        }
        return "流程已经提交";
    }


}
